package com.hup.sidebar.service;

import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.hup.sidebar.SideBarApplication;
import com.hup.sidebar.model.AppConfig;
import com.hup.sidebar.view.touch.ActivationView;

import lombok.Getter;

/**
 * 激活器悬浮窗服务
 *
 * @author hugan
 * @date 2022/6/3
 */
public class ActivationFloaterService {
    private static final String TAG = ActivationFloaterService.class.getSimpleName();

    private final ActivationView activationViewLeft, activationViewRight;
    private final ComponentCallbacks componentCallbacks;
    private int lastOrientation;
    @Getter
    private boolean showing;

    public ActivationFloaterService(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        AppConfig appConfig = SideBarApplication.getBean(AppConfig.class);
        activationViewLeft = new ActivationView(context, windowManager, Gravity.START, appConfig);
        activationViewRight = new ActivationView(context, windowManager, Gravity.END, appConfig);

        componentCallbacks = new ComponentCallbacks() {
            @Override
            public void onConfigurationChanged(@NonNull Configuration configuration) {
                //Log.i(TAG, "onConfigurationChanged: " + configuration.orientation);
                if (lastOrientation != configuration.orientation) {
                    lastOrientation = configuration.orientation;
                    activationViewLeft.onOrientationChanged();
                    activationViewRight.onOrientationChanged();
                }
            }

            @Override
            public void onLowMemory() {

            }
        };
    }

    public void switchService(boolean b) {
        Log.i(TAG, "switchService: new=" + b + ", old=" + showing);
        if (showing == b) return;
        if (b) {
            activationViewLeft.addToScreen();
            activationViewRight.addToScreen();
            SideBarApplication.putBean(this);
            activationViewLeft.getContext().registerComponentCallbacks(componentCallbacks);
        } else {
            SideBarApplication.removeBean(getClass());
            activationViewLeft.removeFromScreen();
            activationViewRight.removeFromScreen();
            activationViewLeft.getContext().unregisterComponentCallbacks(componentCallbacks);
        }
        showing = b;
    }

    public void onActivationConfigUpdated() {
        activationViewLeft.onActivationConfigUpdated();
        activationViewRight.onActivationConfigUpdated();
    }

    public void setActivationVisible(boolean b) {
        activationViewLeft.setVisible(b);
        activationViewRight.setVisible(b);
    }

    public void onPieConfigUpdated() {
        activationViewLeft.getPieView().onPieConfigUpdated();
        activationViewRight.getPieView().onPieConfigUpdated();
    }

}
